package com.lovo.service.impl;

import com.lovo.bean.CommomentBean;
import com.lovo.dao.ICommomentDAO;
import com.lovo.service.ICommomentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bdk
 * @since 2021-03-11
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class CommomentServiceImpl extends ServiceImpl<ICommomentDAO, CommomentBean> implements ICommomentService {
    @Resource
    private ICommomentDAO commomentDAO;
    @Override
    public List selectAllCommoment(Long userId) {
        List<CommomentBean> commomentList = commomentDAO.selectAllCommoment();
        for(CommomentBean commomentBean : commomentList){
            if(userId != null){
                List list = commomentDAO.selectByCommomentIdAdnUserId(commomentBean.getCommomentId(),userId);
                if(list.size() > 0){
                    commomentBean.setDoLike(true);
                }
            }

        }
        return commomentList;
    }

    @Override
    public void doLikeCommoment(Long commomentId,Long userId) {
        CommomentBean commomentBean = commomentDAO.selectById(commomentId);
        commomentBean.setCommomentLikeCount(commomentBean.getCommomentLikeCount() + 1);
        commomentDAO.updateById(commomentBean);
        commomentDAO.insertUserLike(commomentId,userId);
    }

    @Override
    public void cancelLikeCommoment(Long commomentId,Long userId) {
        CommomentBean commomentBean = commomentDAO.selectById(commomentId);
        commomentBean.setCommomentLikeCount(commomentBean.getCommomentLikeCount() - 1);
        commomentDAO.updateById(commomentBean);
        commomentDAO.deleteUserLike(commomentId,userId);
    }
}
